package com.example.demo.controller;

import com.example.demo.config.RateLimiter;
import com.example.demo.model.dto.ActivityDTO;
import com.example.demo.model.enums.ResultCodeEnum;
import com.example.demo.model.request.activity.*;
import com.example.demo.model.response.*;
import com.example.demo.service.ActivityService;
import com.example.demo.util.PageInfo;
import com.example.demo.util.Result;
import com.example.demo.util.ResultGenerator;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/activity")
public class ActivityController {

    @Autowired
    private ActivityService activityService;

    @PostMapping("/create")
    public Result<Boolean> createActivity(@RequestBody CreateActivityRequest request) {
        //参数校验
        if (null == request) {
            return ResultGenerator.genFreeResult(ResultCodeEnum.PARAM_ILEEGAL, false);
        }
        if (StringUtils.isBlank(request.getName()) || StringUtils.isBlank(request.getStartTime()) || StringUtils.isBlank(request.getEndTime()) ||
                request.getChance() <= 0) {
            return ResultGenerator.genFreeResult(ResultCodeEnum.PARAM_ILEEGAL, false);
        }
        return ResultGenerator.genSuccessResult(activityService.create(request));
    }

    @GetMapping("/pageQuery")
    public Result<PageQueryActivityResponse> pageQueryActivity(PageInfo pageInfo) {
        //参数校验
        if (pageInfo == null || pageInfo.getCurrent() < 0 || pageInfo.getPageSize() <= 0) {
            return ResultGenerator.genFreeResult(ResultCodeEnum.PARAM_ILEEGAL, null);
        }
        return ResultGenerator.genSuccessResult(activityService.pageQuery(pageInfo));
    }

    @PostMapping("/delete")
    public Result<Boolean> deleteActivity(@RequestBody DeleteActivityRequest request) {
        //参数校验
        if (null == request || null == request.getActivityId()) {
            return ResultGenerator.genFreeResult(ResultCodeEnum.PARAM_ILEEGAL, false);
        }
        return ResultGenerator.genSuccessResult(activityService.delete(request.getActivityId()));
    }

    @GetMapping("/attend")
    @RateLimiter(20)
    public Result<AttendActivityResponse> attendActivity(AttendActivityRequest request) {
        //参数校验
        if (null == request || null == request.getActivityId() || StringUtils.isBlank(request.getUserName())) {
            return ResultGenerator.genFreeResult(ResultCodeEnum.PARAM_ILEEGAL, null);
        }
        return ResultGenerator.genSuccessResult(activityService.attend(request));
    }

    @GetMapping("/queryRecord")
    public Result<QueryActivityRecordResponse> queryActivityRecord(QueryActivityRecordRequest request) {
        //参数校验
        if (null == request || null == request.getActivityId()) {
            return ResultGenerator.genFreeResult(ResultCodeEnum.PARAM_ILEEGAL, null);
        }
        return ResultGenerator.genSuccessResult(activityService.queryRecord(request));
    }

    @GetMapping("/queryUserPrize")
    public Result<QueryUserPrizeResponse> queryUserPrize(QueryUserPrizeRequest request) {
        //参数校验
        if (null == request || StringUtils.isBlank(request.getUserName()) ) {
            return ResultGenerator.genFreeResult(ResultCodeEnum.PARAM_ILEEGAL, null);
        }
        return ResultGenerator.genSuccessResult(activityService.queryUserPrize(request));
    }

    @PostMapping("/update")
    public Result<Boolean> update(@RequestBody UpdateActivityRequest request) {
        //参数校验
        if (null == request || null == request.getId() || null == request.getName()
                || null == request.getStartTime() || null == request.getEndTime() || null == request.getChance()) {
            return ResultGenerator.genFreeResult(ResultCodeEnum.PARAM_ILEEGAL, null);
        }
        return ResultGenerator.genSuccessResult(activityService.update(request));
    }

    @GetMapping("/query")
    public Result<ActivityDTO> query(Integer id) {
        if (null == id) {
            return ResultGenerator.genFreeResult(ResultCodeEnum.PARAM_ILEEGAL, null);
        }
        return ResultGenerator.genSuccessResult(activityService.query(id));
    }

    @GetMapping("/pageQueryByUser")
    public Result<PageQueryActivityByUserResponse> pageQueryByUser(PageQueryActivityByUserRequest request) {
        if (null == request || null == request.getCurrent() || null == request.getPageSize() || StringUtils.isBlank(request.getUserName())) {
            return ResultGenerator.genFreeResult(ResultCodeEnum.PARAM_ILEEGAL, null);
        }
        return ResultGenerator.genSuccessResult(activityService.pageQueryByUser(request));
    }
}
